import 'dart:async';
import 'dart:math';

import 'package:flutter/material.dart';
import 'package:view_lamp/custom/view_lamp_item.dart';

import 'lamp_widget.dart';

///波浪模式
///黄云音
///2020年3月1日

class CustomViewLamp6 extends StatefulWidget {
  final double multiColorWidth;
  final double multiColorHeight;
  final List<Color> meColorList;
  final Color bgcolor;
  const CustomViewLamp6({
    Key key,
    this.multiColorWidth,
    this.multiColorHeight,
    this.bgcolor,
    this.meColorList,
  }) : super(key: key);

  @override
  _CustomViewLamp6State createState() => _CustomViewLamp6State();
}

class _CustomViewLamp6State extends State<CustomViewLamp6>
    with SingleTickerProviderStateMixin {
  List<Map<String,int>> indexList = [
    {'first':0,'last':7}, {'first':6,'last':3}, {'first':2,'last':9}, {'first':10,'last':5}, {'first':8,'last':11}, {'first':4,'last':1}];
  Timer timer;

  List<ViewLampItem> lampItemInfoList = [
    ViewLampItem(17, 72, Colors.transparent, 'assets/images/previewbulb1-x17y72.png'),
    ViewLampItem(82, 90, Colors.transparent, 'assets/images/previewbulb2-x82y90.png'),
    ViewLampItem(147, 104, Colors.transparent, 'assets/images/previewbulb3-x147y104.png'),
    ViewLampItem(213, 106, Colors.transparent, 'assets/images/previewbulb4-x213y106.png'),
    ViewLampItem(279, 101, Colors.transparent, 'assets/images/previewbulb5-x279y101.png'),
    ViewLampItem(344, 90, Colors.transparent, 'assets/images/previewbulb6-x344y90.png'),
    ViewLampItem(409, 73, Colors.transparent, 'assets/images/previewbulb7-x409y73.png'),
    ViewLampItem(474, 60, Colors.transparent, 'assets/images/previewbulb8-x474y60.png'),
    ViewLampItem(540, 60, Colors.transparent, 'assets/images/previewbulb9-x540y60.png'),
    ViewLampItem(606, 70, Colors.transparent, 'assets/images/previewbulb10-x606y70.png'),
    ViewLampItem(670, 87, Colors.transparent, 'assets/images/previewbulb11-x670y87.png'),
    ViewLampItem(736, 102, Colors.transparent, 'assets/images/previewbulb12-x736y102.png'),
  ];
  AnimationController animationController;
  List<Map<String, Color>> gradientPair = [];
  int currentIndex = 0;
  @override
  void initState() {
    super.initState();
    animationController =
        AnimationController(vsync: this, duration: Duration(seconds: 1));
    animationController.addListener(() {
      setState(() {
        // changeItemColor();
        _doAction();
      });
    });
    animationController.addStatusListener((status) {
      if (status == AnimationStatus.completed) {
        setState(() {
          if (currentIndex >= indexList.length - 1) {
            currentIndex = 0;
          } else {
            currentIndex++;
          }
          lampItemInfoList.forEach((element) { element.color = widget.bgcolor;});
          animationController.forward(from: .0);
        });
      }
    });
    animationController.forward();
  }


  void _doAction() {
    Color currentColor = Color.lerp(widget.meColorList[currentIndex], widget.bgcolor, animationController.value);
    int firstIndex = indexList[currentIndex]['first'];
    int secondIndex = indexList[currentIndex]['last'];
    lampItemInfoList[firstIndex].color = currentColor;
    lampItemInfoList[secondIndex].color = currentColor;
  }

  void changeItemColor() {
    double value = animationController.value;
    lampItemInfoList.forEach((element) {
      element.color = widget.bgcolor;
    });
    List<Map<String,int>> indexList = [
      {'first':0,'last':7}, {'first':6,'last':3}, {'first':2,'last':9}, {'first':10,'last':5}, {'first':8,'last':11}, {'first':4,'last':1}];

    for (int i = 0; i < lampItemInfoList.length; i++) {
      if(i == indexList[currentIndex]['first'] || i == indexList[currentIndex]['last']){
        var element = gradientPair[i];

        Color begin = element['begin'];
        Color end = element['end'];

        Color valueColor = ColorTween(begin: begin, end: end).lerp(value);
        lampItemInfoList[indexList[currentIndex]['first']].color = valueColor;
        lampItemInfoList[indexList[currentIndex]['last']].color = valueColor;
      }else{
        lampItemInfoList[i].color =  widget.bgcolor;
      }
    }

    currentIndex++;
    //还原currentIndex，以此循环
    if (currentIndex == 6) {
      currentIndex = 0;
    }
  }

  @override
  void dispose() {
    timer?.cancel();
    animationController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return LightChainWidget(lampItemInfoList: lampItemInfoList, multiColorWidth: widget.multiColorWidth,multiColorHeight:widget.multiColorHeight);
  }
}
